package com.xuerhua.yxt.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xuerhua.yxt.dao.StudentInfoMapper;
import com.xuerhua.yxt.model.StudentInfo;
import com.xuerhua.yxt.model.StudentOrganize;
import com.xuerhua.yxt.page.Page;
import com.xuerhua.yxt.reqVo.BaseQueryVO;
import com.xuerhua.yxt.reqVo.StudentReqVO;
import com.xuerhua.yxt.service.IStudentService;
import com.xuerhua.yxt.utils.BeanUtils;
import com.xuerhua.yxt.utils.StringUtil;
import com.xuerhua.yxt.view.StudentBindView;
import com.xuerhua.yxt.view.StudentInfoView;
import com.xuerhua.yxt.view.StudentInputTotalView;
import com.xuerhua.yxt.view.StudentPersonImportView;
import com.xuerhua.yxt.view.StudentTotalView;

@Service("studentService")
public class StudentServiceImpl implements IStudentService {

	@Autowired
	private StudentInfoMapper studentInfoMapper;

	
	@Override
	public int delByIds(List<String> list) {
		return studentInfoMapper.delByIds(list);
	}

	@Override
	public int insertBatch(List<StudentInfo> list) {
		return studentInfoMapper.insertBatch(list);
	}

	@Override
	public List<StudentInfo> selectByPage(Page<StudentInfo> page) {
		return studentInfoMapper.selectByPage(page);
	}

	@Override
	public List<StudentPersonImportView> selectPersonImport(Page<StudentPersonImportView> page) {
		return studentInfoMapper.selectPersonImport(page);
	}

	@Override
	public List<StudentInfo> selectFullListByPage(Page<StudentInfo> page) {
		return studentInfoMapper.selectFullListByPage(page);
	}

	@Override
	public StudentInfo selectById(Integer id) {
		return studentInfoMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateById(StudentInfo record) {
		return studentInfoMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public List<StudentInfo> selectByOpenid(String openid) {
		return studentInfoMapper.selectByOpenid(openid);
	}

	@Override
	public List<StudentInfoView> selectByPageForBind(Page<StudentInfoView> page) {
		return studentInfoMapper.selectByPageForBind(page);
	}

	@Override
	public List<StudentOrganize> fillStudentOrganize(Integer finishGrade, Integer schoolId) {
		List<StudentOrganize> list = studentInfoMapper.selectStudentOrganize(finishGrade,schoolId);
		Map<String, List<StudentOrganize>> gradeMap = new HashMap<String, List<StudentOrganize>>();
		List<StudentOrganize> gradeList = new ArrayList<>();
		if(list != null) {
			for(StudentOrganize menu : list) {
				String key = String.valueOf(menu.getGrade());
				List<StudentOrganize> childMenuList = gradeMap.get(key);
				if(childMenuList == null) {
					childMenuList = new ArrayList<StudentOrganize>();
					StudentOrganize organize = BeanUtils.copyAs(menu, StudentOrganize.class);
					gradeList.add(organize);
				}
				childMenuList.add(menu);
				gradeMap.put(key, childMenuList);
			}
			
			for(StudentOrganize menu : gradeList) {
				List<StudentOrganize> list2 = gradeMap.get(String.valueOf(menu.getGrade()));
				menu.setSubList(list2);
				if(!StringUtil.isNullOrEmpty(list2)) {
					int count = 0;
					for(StudentOrganize student : list2) {
						count += student.getStudentCount();
					}
					menu.setStudentCount(count);
					menu.setHasChild(true);
					menu.setClazz(null);
				}
			}
			gradeMap = null;
			return gradeList;
		}
		return null;
	}

	@Override
	public List<StudentInfo> selectListByParam(StudentReqVO record) {
		return studentInfoMapper.selectListByParam(record);
	}

	@Override
	public List<StudentOrganize> selectStudentOrganize(Integer finishGrade, Integer schoolId) {
		return studentInfoMapper.selectStudentOrganize(finishGrade,schoolId);
	}

	@Override
	public StudentInfo selectStudentBindParent(Integer studentId, String openid) {
		return studentInfoMapper.selectStudentBindParent(studentId, openid);
	}

	@Override
	public List<StudentInfoView> selectByKeywordForBind(StudentReqVO reqVO) {
		return studentInfoMapper.selectByKeywordForBind(reqVO);
	}
	
	@Override
	public List<StudentInfoView> selectByKeyword(StudentReqVO reqVO) {
		return studentInfoMapper.selectByKeyword(reqVO);
	}

	@Override
	public List<StudentInfo> selectListByIds(String[] ids) {
		return studentInfoMapper.selectListByIds(ids);
	}

	@Override
	public int updatePoints(Integer id, Float point) {
		int result = startUpdatePoints(id, point);
		if(result == 0) {
			for(int i = 0; i < 2; i++) {
				result = startUpdatePoints(id, point);
				if(result == 1) {
					break;
				}
			}
		}
		return result;
	}

	private int startUpdatePoints(Integer id, Float point) {
		StudentInfo student = studentInfoMapper.selectByPrimaryKey(id);
		Float points = student.getPoints() == null ? 0 : student.getPoints();
		student.setPoints(points + point);
		return studentInfoMapper.updateForVersionById(student);
	}

	@Override
	public List<StudentInfo> selectNoInputList(BaseQueryVO vo) {
		return studentInfoMapper.selectNoInputList(vo);
	}

	@Override
	public List<StudentInputTotalView> selectInputTotal(BaseQueryVO vo) {
		return studentInfoMapper.selectInputTotal(vo);
	}

	@Override
	public List<StudentInfo> selectNoFaceCollectionList(BaseQueryVO vo) {
		return studentInfoMapper.selectNoFaceCollectionList(vo);
	}

	@Override
	public List<StudentInputTotalView> selectFaceCollectionTotal(BaseQueryVO vo) {
		return studentInfoMapper.selectFaceCollectionTotal(vo);
	}

	@Override
	public int addStudent(StudentInfo record) {
		BaseQueryVO vo = new BaseQueryVO();
		vo.setGrade(record.getGrade());
		vo.setClazz(record.getClazz());
		vo.setSchoolId(record.getSchoolId());
		int count = studentInfoMapper.selectClazzStudentCount(vo);
		count = count + 1;
		record.setStudyNo("" + record.getGrade() + 
    			(record.getClazz() < 10 ? "0" + record.getClazz() : record.getClass())+
    			(count < 10 ? "0" + count : count));
		return studentInfoMapper.insertSelective(record);
	}

	@Override
	public List<StudentTotalView> selectStudentTotal(Page<StudentTotalView> page) {
		return studentInfoMapper.selectStudentTotal(page);
	}

	@Override
	public List<StudentBindView> selectBindTotal(BaseQueryVO vo) {
		return studentInfoMapper.selectBindTotal(vo);
	}

	@Override
	public List<StudentInfo> selectNoBindList(BaseQueryVO vo) {
		return studentInfoMapper.selectNoBindList(vo);
	}

	@Override
	public List<Integer> getFullGrade() {
		return studentInfoMapper.getFullGrade();
	}

	@Override
	public List<Integer> getFullClazz(Integer grade) {
		return studentInfoMapper.getFullClazz(grade);
	}
	
}
